package com.avengers.core.model;

import com.avengers.core.bean.enums.SymbolEnum;
import com.avengers.core.kit.IpKit;
import com.avengers.core.kit.StringKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @author yu.wang
 * @since 2019/3/11 3:48 PM
 **/
public class ScopeChecker {
    private static final Logger logger = LoggerFactory.getLogger(ScopeChecker.class);
    private static String scope = null;
    private static final String SCOPE_DEV = "dev";
    private static String currentIp = null;
    private static final Set<String> allowIpList = new HashSet<>();

    private final String scopePropName;

    public ScopeChecker(String scopePropName) {
        this.scopePropName = scopePropName;
        checkScope();
    }

    public Set<String> getAllowIpList() {
        return allowIpList;
    }

    private String checkScope() {
        if (null == scope) {
            if (null == scopePropName || scopePropName.isEmpty()) {
                scope = "";
            } else {
                Prop prop = PropKit.use(scopePropName);
                // 默认为开发环境
                scope = SCOPE_DEV;
                try {
                    currentIp = IpKit.getRealIp();
                    allowIpList.add(currentIp);
                    logger.info("environment ip : 【{}】", currentIp);
                    Properties properties = prop.getProperties();
                    for (Object name : properties.keySet()) {
                        String ipStr = prop.get(name.toString());
                        if (StringKit.notBlank(ipStr)) {
                            for (String scopeIp : ipStr.split(SymbolEnum.comma.string)) {
                                if (Objects.equals(scopeIp, currentIp)) {
                                    scope = name.toString();
                                    logger.info("Loading scope : {}", scope);
                                    return scope;
                                }
                            }
                        }
                    }
                    logger.info("the ip【{}】 not found in scope file【{}】, use default scope【{}】", currentIp, scopePropName, scope);
                } finally {
                    String ips = prop.get(scope);
                    if (StringKit.notBlank(ips)) {
                        allowIpList.addAll(Arrays.asList(ips.split(SymbolEnum.comma.string)));
                    }
                    PropKit.useless(scopePropName);
                }
            }
        }
        return scope;
    }

    public String getFullPropPath(String fileName) {
        String fullPath = fileName;
        String scope = checkScope();
        try {
            if (SCOPE_DEV.equals(scope)) {
                String userName = System.getProperty("user.name");
                Prop prop = PropKit.use("local/" + userName + "/" + fileName);
                if (null != prop && null != prop.getProperties() && !prop.getProperties().isEmpty()) {
                    fullPath = "local/" + userName + "/" + fileName;
                }
            }
        } catch (Exception ignore) {
        }
        if (fileName.equals(fullPath)) {
            if (StringKit.notBlank(scope)) {
                fullPath = scope + "/" + fileName;
            }
        }
        logger.info("load prop file:【{}】", fullPath);
        return fullPath;
    }

    public static String getScope() {
        return scope;
    }

    public static String getCurrentIp() {
        return currentIp;
    }
}
